﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication3.权限管理
{
    public partial class editStudents : Form
    {
        public string myEdit_ID, myEdit_name, myEdit_passwd, myEdit_pressions, myEdit_departments, myEdit_classes;

        public editStudents()
        {
            InitializeComponent();
        }
        //填充信息
        public void fill_Info()
        {
            this.editStudents_ID.Text           = myEdit_ID;
            this.editStudents_name.Text         = myEdit_name;
            this.editStudents_passwd.Text       = myEdit_passwd;
            this.editStudents_pressions.Text    = myEdit_pressions;
            this.editStudents_departments.Text  = myEdit_departments;
            this.editStudents_classes.Text      = myEdit_classes;
        }
        //删除表中该条信息
        public void del_Info()
        {
            权限管理.permissionManaging.mySql.sql_search_database("delete from studentstable  where studentsID = '"
                    + myEdit_ID + "'");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //更新信息
                string updateID, updateName, updatePasswd, updatePressions, updatedepartments, updateClasses;
                updateID = editStudents_ID.Text.Trim();
                updateName = editStudents_name.Text.Trim();
                updatePasswd = editStudents_passwd.Text.Trim();
                updatePressions = editStudents_pressions.Text.Trim();
                updatedepartments = editStudents_departments.Text.Trim();
                updateClasses = editStudents_classes.Text.Trim();

                DataSet ds = 权限管理.permissionManaging.mySql.sql_search_database("select * from studentstable "
                        + "where studentsID= '" + updateID + "' ");
                int rowcount = ds.Tables[0].Rows.Count;
                if (rowcount != 0) { MessageBox.Show("ID  have existed"); return; };

                if (updateID == "") { MessageBox.Show("学生ID不能为空！"); return; }
                if (updateName == "") { MessageBox.Show("学生姓名不能为空！"); return; }
                if (updatePasswd == "") { MessageBox.Show("学生密码不能为空！"); return; }
                if (updatePressions == "") { MessageBox.Show("学生权限不能为空！"); return; }
                if (updatedepartments == "") { MessageBox.Show("学生院系不能为空！"); return; }
                if (updateClasses == "") { MessageBox.Show("学生班级不能为空！"); return; }

                //插入数据库          

                权限管理.permissionManaging.mySql.excute_sql("update studentstable set "
                    + "studentsID = '" + updateID + "', "
                    + "username = '" + updateName + "', "
                    + "passwd = '" + updatePasswd + "', "
                    + "pression = '" + updatePressions + "', "
                    + "departments = '" + updatedepartments + "', "
                    + "classes = '" + updateClasses + "' "
                    + "where studentsID = '" + myEdit_ID +"' ");
                //Debug.WriteLine("update studentstable set "
                //    + "studentsID = '" + updateID + "', "
                //    + "username = '" + updateName + "', "
                //    + "passwd = '" + updatePasswd + "', "
                //    + "pression = '" + updatePressions + "', "
                //    + "departments = '" + updatedepartments + "', "
                //    + "classes = '" + updateClasses + "' "
                //    + "where studentsID = '" + myEdit_ID + "' ");

                权限管理.permissionManaging.mySql.excute_sql("update sqlTableName_and_IDtable set userID = '" + updateID + "' where "
                    + "userID = '" + myEdit_ID + "'");
         
                this.Dispose();
            }
            catch
            {
                MessageBox.Show("添加过程中出现错误！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //插入数据库
            权限管理.permissionManaging.mySql.excute_sql("update studentstable set "
                    + "studentsID = '" + myEdit_ID + "', "
                    + "username = '" + myEdit_name + "', "
                    + "passwd = '" + myEdit_passwd + "', "
                    + "pression = '" + myEdit_pressions + "', "
                    + "departments = '" + myEdit_departments + "', "
                    + "classes = '" + myEdit_classes + "' "
                    + "where studentsID = '" + myEdit_ID + "' ");
            this.Dispose();
        }
    }
}
